package com.woniuxy.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 曾令忠
 * @Description: 死信的处理
 * @Date: 2020/7/7 0007-上午 9:44
 */
@Configuration
public class DeadLetterConf {

    //正常的业务队列
    @Bean
    public Queue businessQueue(){
        Map<String,Object> map = new HashMap<>();
        //绑定死信交换机
        map.put("x-dead-letter-exchange","dead-letter-exchange");
        //绑定当前队列死信的路由
        map.put("x-dead-letter-routing-key","dead-key");
        //创建队列
        return new Queue("businessQueue",true,false,false,map);
    }
    //正常的交换机
    @Bean
    public DirectExchange businessExchange(){
        return new DirectExchange("businessExchange");
    }
    //死信队列
    @Bean
    public Queue deadLetterQueue(){
        return new Queue("deadLetterQueue");
    }
    //死信交换机
    @Bean
    public DirectExchange deadLetterExchange(){
        return new DirectExchange("dead-letter-exchange");//此处的交换机必须与正常队列中绑定的死信交换机的名字一样
    }
    //绑定正常交换机和队列
    @Bean
    public Binding bindingBusinessQueueAndExchange(Queue businessQueue,DirectExchange businessExchange){
        return BindingBuilder.bind(businessQueue).to(businessExchange).with("business");
    }
    //绑定死信交换机和死信队列
    @Bean
    public Binding bindingDeadQueueAndExchange(Queue deadLetterQueue,DirectExchange deadLetterExchange){
        //此处的路由要与正常队列中指定的死信队列的路由一样
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with("dead-key");
    }

}
